package cn.hackcloud.mybatis.plugin;

import java.sql.Statement;
import java.util.Properties;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;

@Intercepts(value = { @Signature(type = StatementHandler.class, method = "parameterize", args = Statement.class) })
public class MyFirstPlugin implements Interceptor {
	/**
	 * 拦截目标对象的目标方法的执行
	 */
	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		System.out.println("MyFirstPlugin...intercept:"+invocation.getMethod());
		Object object = invocation.proceed();
		return object;
	}

	/**
	 * 包装目标对象，为目标对象创建一个代理对象
	 */
	@Override
	public Object plugin(Object target) {
		Object object = Plugin.wrap(target, this);
		// 我们可以借助Plugin的wrap方法来使用当前Interceptor包装我们目标对象
		System.out.println("MyFirstPlugin...plugin:mybatis将要包装的对象" + target);
		return object;
	}

	@Override
	public void setProperties(Properties properties) {
		System.out.println("插件配置的信息：" + properties);
	}

}
